//
// Created by bjliuyong on 2021/7/11.
//

#ifndef GC_CLASSFILESTREAM_H
#define GC_CLASSFILESTREAM_H

#include "os/bytes.h"
#include "utilities/top.h"


class ClassFileStream {
private:
    u1*   _buffer_start; // Buffer bottom
    u1*   _buffer_end;   // Buffer top (one past last element)
    u1*  _current; // Current buffer position
    const char* _source; // Source of stream 

public:
    // Constructor
    ClassFileStream(u1* buffer, int length, const char* source);
    // Buffer access
    u1* buffer() const           { return _buffer_start; }
    int length() const           { return _buffer_end - _buffer_start; }
    u1* current() const    { return _current; }
    void set_current(u1* pos)    { _current = pos; }
    const char* source() const   { return _source; }

    // Read u1 from stream
    u1 get_u1_fast() {
        return *_current++;
    }

    u2 get_u2_fast(){
        u2 res = Bytes::get_Java_u2((address)_current);
        _current += 2;
        return res ;
    }

    // Read u4 from stream
    //u4 get_u4(TRAPS);
    u4 get_u4_fast() {
        u4 res = Bytes::get_Java_u4((address)_current);
        _current += 4;
        return res;
    }

    u8 get_u8_fast(){
        u8 res = Bytes::get_Java_u8((address)_current);
        _current += 8;
        return res;
    }

    void skip_u1_fast(int length) {
        _current += length;
    }

    void copy(char* dst,int length){
        memcpy(dst,_current,length);
        skip_u1_fast(length);
    }

};

#endif //GC_CLASSFILESTREAM_H
